<?php
/**
 * Created by PhpStorm.
 * User: hengsu
 * Date: 2016/5/27
 * Time: 10:23
 */

namespace Home\Controller;
use Think\Controller;

class DatumController extends Controller{
    /*********考试押秘*********/
    public function testKeyList(){
        $testKeyOrder=M('Testkey_order');$testKey=M('Testkey');
        $catId = isset($_POST['catId'])?trim($_POST['catId']):null;       //分类id
        $userId = isset($_POST['userId'])?trim($_POST['userId']):null;       //用户id
        $type=isset($_POST['type'])?trim($_POST['type']):1;       //类型
        $page=isset($_POST['page'])?trim($_POST['page']):1;       //类型
        $ketIdList=$testKeyOrder->field('key_id')->where('user_id='.$userId)->select();
		if(!empty($ketIdList)){
			foreach ($ketIdList as $k =>$v){
				$keystr.=$v['key_id'].',';
			}
			$keystr=trim($keystr,',');
			$keyId=explode(',',$keystr);
			if($type == 1){
				$where['id']=array('not in',$keyId);
			}else{
				$where['id']=array('in',$keyId);
			}
			$where['cata_id'] = $catId;
			$testKeyList=$testKey->field('id,title,price')->page($page,10)->where($where)->select();
			if(empty($testKeyList)){
				$requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
			}else{
				$requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>$testKeyList);
			}
			echo json_encode($requset); exit;
		}else{
			if($type == 1){
				$where['cata_id'] = $catId;
				$testKeyList=$testKey->field('id,title,price')->page($page,10)->where($where)->select();
				if(empty($testKeyList)){
					$requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
				}else{
					$requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>$testKeyList);
				}
			}else{
				$requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
			}
			echo json_encode($requset); exit;
		}
    }
    /***********押秘详情***********/
    public function testKeyInfo(){
        $testKey=M('Testkey');
        $keyId = isset($_POST['keyId'])?trim($_POST['keyId']):null;
        $where['id'] = $keyId;
        $testKeyLi=$testKey->field('content')->where($where)->find();
        if(empty($testKeyLi)){
            $requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
        }else{
            $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>array('contet'=>$testKeyLi['content']));
        }
        echo json_encode($requset); exit;
    }
    public function getPrice(){
        $catId = isset($_POST['catId'])?trim($_POST['catId']):null;       //分类id
        $type = isset($_POST['type'])?trim($_POST['type']):1;       //分类id
        $price=M('Price');
        $priceli=$price->where('cate_id='.$catId)->find();
        if(!empty($priceli)){
            if($type == 1){
                $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>array('price'=>$priceli['exam_price']));
            }else{
                $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>array('price'=>$priceli['reference_price']));
            }
        }else{
            $requset=array('returnCode'=>999,'returnMsg'=>'暂时没有价格');
        }
        echo json_encode($requset);
    }
    /**********资料文库**********/
    public function datumList(){
        $catId = isset($_POST['catId'])?trim($_POST['catId']):null;       //分类id
        $userId = isset($_POST['userId'])?trim($_POST['userId']):null;       //用户id
        $datumType=isset($_POST['datumType'])?trim($_POST['datumType']):null;       //类型
        $page=isset($_POST['page'])?trim($_POST['page']):null;       //类型
        $datumOrder=M('Datum_order');
        $orderList=$datumOrder->where(array('user_id'=>$userId,'cat_id'=>$catId))->find();
        if(!empty($orderList)){
            if($orderList['status'] == 1){
                $status=1;
            }else{
                $status=0;
            }
        }else{
            $status=0;
        }
        if(!empty($datumType)){
            $where['type']=$datumType;
        }
        $where['cata_two'] = $catId;
        $reference=M('Reference');
        $referenceList=$reference->field('id,name,cover,about,addtime')->page($page,10)->where($where)->select();
        foreach ($referenceList as $key => $val){
            $referenceList[$key]['status']=$status;
        }
        if(empty($referenceList)){
            $requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
        }else{
            $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>$referenceList);
        }
        echo json_encode($requset); exit;
    }
    /***********文库详情***********/
    public function datumInfo(){
        $datum=M('Reference');
        $datumId = isset($_POST['datumId'])?trim($_POST['datumId']):null;
        $where['id'] = $datumId;
        $datumli=$datum->field('content')->where($where)->find();
        if(empty($datumli)){
            $requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
        }else{
            $datum->where($where)->setInc('readCount');
            $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>array('contet'=>$datumli['content']));
        }
        echo json_encode($requset); exit;
    }
    /*************资料文库标签**************/
    public function datumType(){
        $catId = isset($_POST['catId'])?trim($_POST['catId']):null;       //分类id
        $referencetype=M('Referencetype');
        $referencetypeList=$referencetype->where('cate_id='.$catId)->select();
        if(empty($referencetypeList)){
            $requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
        }else{
            $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>$referencetypeList);
        }
        echo json_encode($requset); exit;
    }
    /*************资料文库简介**************/
    public function datumTroduce(){
        $datumId = isset($_POST['datumId'])?trim($_POST['datumId']):null;
        if($datumId==null){
            $requset=array('returnCode'=>996,'returnMsg'=>'请求数据为空','returnData'=>null);
        }else{
            $info=M('reference')->where(['id'=>$datumId])->field('name,cover,about,addtime,readCount')->find();
            if(empty($info)){
                $requset=array('returnCode'=>0,'returnMsg'=>'返回成功','returnData'=>$info);
            }else{
                $requset=array('returnCode'=>999,'returnMsg'=>'空数据','returnData'=>null);
            }
        }
    }
}